/*
作者：江怀玉
日期：2014-06-10
功能：弹出菜单
解决：
-------------------------------------
依赖JS:
jquery,jquery.data
依赖CSS：

---------------------------------------------------------*/
(function ($) {

    $.fn.menu = function () {
        //debugger
        var defaults = {
        };
        $(this).hover(HightDialogBulider, function () { }
        );
        $this = $(this);
        fid = null;
        function HightDialogBulider() {
            $(".div_main_menu").hide();
            fid = $(this).attr("id");
            $(".menuSelected").removeClass("menuSelected");
            $(this).addClass("menuSelected");
            var X = $(this).offset().top;
            var Y = $(this).offset().left;
            if ($("#div_main_menu" + fid).attr("id") == undefined) {
                $('body').append('<div class="div_main_menu" id="div_main_menu' + fid + '" style="z-index: 999;border: 1px solid #A8A8A8;border-top: 1px solid #A8A8A8;width:400px;height:300px;position: absolute; background-color: #fff; display: none;">' +
                '</div>');
                $("#div_main_menu" + fid).height(400);
                $("#div_main_menu" + fid).width(500);
                $("#div_main_menu" + fid).hover(function () {
                },
                function () {
                    $(".div_main_menu").hide();
                    $this.removeClass("menuSelected");
                });
                $("#div_main_menu" + fid).css("left", Y).css("top", X + ($(this).height() - 0));
                if ($.trim($("#div_main_menu" + fid).text()) == "") {
                    //debugger
                    getModuelMenuData();
                }
            }
            $("#div_main_menu" + fid).show();
        }

        //获取模块菜单数据
        function getModuelMenuData() {
            var nav = initModuleMenu(_userdata, fid, 0);
            if ($.trim($("#div_main_menu" + fid).text()) == "") {
                $("#div_main_menu" + fid).html(nav);
                $("#div_main_menu" + fid + " li").bind("click", function () {
                    var url = $(this).attr("url");
                    //debugger
                    if ($.trim(url) == "") {
                        return;
                    }
                    //url = url.indexOf("?") > 0 ? (url + "&functionid=" + fid) : (url + "?functionid=" + fid);
                    var title = $(this).attr("title");
                    addTabs(title, url);
                    $(".div_main_menu").hide();
                });
            }

        }

        //初始化子菜单
        function initModuleMenu(data, fid, deep) {
            var _template = "";
            if (deep == 0) {
                _template = '<LI class="menu_item_1" url="[@data.URL@]" id="[@id@]" title="[@data.Name@]"><h4><A href="javascript:;"><I class="icon-personal"></I></A><A class="app-name deep0" >[@app-name@]</A></h4>[@Template@]</LI>';
            } else {
               _template =  '<LI class="menu_item_2" url="[@data.URL@]" id="[@id@]" title="[@data.Name@]"><A href="javascript:;"><I class="icon-personal"><img src="/images/system/prg.png"/></I></A><A class="app-name deep' + deep + '" >[@app-name@]</A>[@Template@]</LI>';
            }
            var _list = "";
            var _data = [];
            for (var i = 0; i < data.length; i++) {
                var obj = data[i];
                if (obj.ParentID.toLocaleLowerCase() == fid.toLocaleLowerCase()) {
                    var _sub = "";
                    if (deep < 1) {
                        _sub = initModuleMenu(data, obj.FunctionId, deep + 1);
                    }
                    var url = "";
                    if ($.trim(obj.URL) != "") {
                        url = obj.URL.indexOf("?") > 0 ? (obj.URL + "&functionid=" + obj.FunctionId) : (obj.URL + "?functionid=" + obj.FunctionId);
                    }
                    _list = _list + _template.replace("[@app-name@]", obj.Name).replace("[@Template@]", _sub).replace("[@data.URL@]", url).replace("[@data.Name@]", obj.Name).replace("[@id@]", obj.FunctionId);
                }
            }
            var nav = "<ul id=\"tb_menu_list" + fid + "\" class=\"main-application-list clearfix\">" + _list + "</ul>";
            return nav;
        }
    }
})(jQuery);
